home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / wb / czesc_4 / tinymeter / source / tinymeter_main / idle.c < prev    next >
C/C++ Source or Header  |  1995-12-20  |  5KB  |  221 lines

  1. /*****************************************************************
  2.  
  3.     SystemIdler V1.0 (Idle counter module for TinyMeter 3.6)  by
  4.     Tinic Urou in 1995, FreeWare, orginal code by Thomas Radtke.
  5.     Use this at your own risk. Please leave me a mail if you are
  6.     using this code in your programs:
  7.  
  8.         EMail: tinic@tinic.mayn.sub.de
  9.  
  10.     I modified the code from cpuload2. The routines to count the
  11.     maximum  are  much  more  better I think, since they use the
  12.     exact same  number  of  cycles  as  the  normal  idle  count
  13.     routine.
  14.     On a A4000/030, disabling the startup-sequence, you will now
  15.     get  1%  usage.  With  all my tools I get 9-12% usage. These
  16.     results should be correct.
  17.  
  18.     Bugs: None known.
  19.     ¯¯¯¯
  20.     Invoking:
  21.     ¯¯¯¯¯¯¯¯
  22.     init_idle(); to setup the idle task
  23.     free_idle(); to remove the idle Task
  24.  
  25.     unsigned long idle;      is the actual idlecount
  26.     unsigned long maximum;   is the maximum idlecount
  27.  
  28.     To get f.ex. the percentage of system usage simply use:
  29.  
  30.     __________________________________________________________
  31.  
  32.         extern unsigned long maximum,idle;
  33.  
  34.         showusage()
  35.         {
  36.             int percent,n;
  37.  
  38.             if(init_idle())
  39.             {
  40.                 for(n=0;n<25;n++)
  41.                 {
  42.                     percent=(int)((idle*100)/maximum);
  43.                     printf("%d percent free\n",percent);
  44.                     Delay(50L);
  45.                 }
  46.                 free_idle();
  47.             }
  48.         }
  49.  
  50.     ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  51.  
  52.     Used compiler:  gcc  2.7.0  with  libnix  1.0  Look  at  the
  53.     makefile for the used options.
  54.  
  55. *******************************************************************/
  56.  
  57. #include <intuition/IntuitionBase.h>
  58. #include <exec/nodes.h>
  59. #include <exec/tasks.h>
  60. #include <libraries/dos.h>
  61. #include <exec/types.h>
  62. #include <exec/memory.h>
  63. #include <stdlib.h>
  64. #include <stdio.h>
  65. #include <signal.h>
  66. #include <exec/libraries.h>
  67. #include <dos/dos.h>
  68.  
  69. extern  struct          Task *FindTask(char *);
  70.         struct          Task *task2,*met;
  71.  
  72.         BOOL            quit_setidle,
  73.                         quit_getidle,
  74.                         start_count=TRUE;
  75.  
  76.         unsigned long   maximum,
  77.                         cnt,
  78.                         idle;
  79.  
  80. getidle()
  81. {
  82.     struct          timeval updateval;
  83.     int             n;
  84.     struct MsgPort  *timerport;
  85.     struct          timerequest *tr;
  86.     struct          Task *met2;
  87.  
  88.     /* use "__geta4 getidle()" for DICE or SAS and remove geta4(); */
  89.  
  90.     geta4();
  91.  
  92.     met2=FindTask(NULL);
  93.  
  94.     if ((timerport=(struct MsgPort *)CreatePort (0,0)))
  95.     {
  96.         if ((tr=(struct timerequest *)CreateExtIO(timerport,sizeof(struct timerequest))))
  97.         {
  98.             if ((OpenDevice (TIMERNAME,UNIT_MICROHZ,(struct IORequest *)tr,0))!=0)
  99.             {
  100.                 DeleteExtIO(tr);
  101.                 DeletePort(timerport);
  102.                 goto error;
  103.             }
  104.         }
  105.         else
  106.         {
  107.             DeletePort(timerport);
  108.             goto error;
  109.         }
  110.     }
  111.     else goto error;
  112.  
  113.     updateval.tv_secs =1;
  114.     updateval.tv_micro=0;
  115.  
  116.     while((met2->tc_SigRecvd & SIGBREAKF_CTRL_D)==0)
  117.     {
  118.         /* signal setidle()-task, that we can start counting */
  119.  
  120.         start_count=FALSE;
  121.  
  122.         cnt=0;
  123.         tr->tr_node.io_Command=TR_ADDREQUEST;
  124.         tr->tr_time=updateval;
  125.         DoIO((struct IORequest *)tr);
  126.         idle=cnt;
  127.  
  128.         /* check if we have to setup maximum */
  129.         if(maximum==0)
  130.         {
  131.             maximum=idle;
  132.             Signal(met,SIGBREAKF_CTRL_D);
  133.         }
  134.     }
  135.  
  136.     CloseDevice(tr);
  137.     DeleteExtIO(tr);
  138.     DeletePort(timerport);
  139.  
  140.     error:
  141.  
  142.     quit_getidle=FALSE;
  143.     start_count =FALSE;
  144.  
  145.     idle   =100000;    /* to avoid divisions by zero from the application */
  146.     maximum=100000;
  147.  
  148.     /* Do nothing and wait for DeleteTask() */
  149.  
  150.     Wait(0L);
  151.  
  152. }
  153.  
  154. setidle()
  155. {
  156.     char *taskname_1 = "CPU_GET";
  157.     struct Task *task;
  158.  
  159.     /* use "__geta4 setidle()" for DICE or SAS and remove geta4(); */
  160.     geta4();
  161.  
  162.     if(met=FindTask(NULL))
  163.     if(task=(struct Task *)CreateTask(taskname_1,127,getidle,4096))
  164.     {
  165.         quit_getidle=TRUE;
  166.  
  167.         /* Wait for beginning. Allocating a timerequest may take a while */
  168.  
  169.         while (start_count) cnt=0;                                         
  170.  
  171.         /* maximum counter */
  172.         while ((met->tc_SigRecvd & SIGBREAKF_CTRL_D)==0) cnt++;
  173.         SetSignal(0,SIGBREAKF_CTRL_D);
  174.  
  175.         met->tc_Node.ln_Pri=-127;
  176.  
  177.         /* idle counter */
  178.         while ((met->tc_SigRecvd & SIGBREAKF_CTRL_D)==0) cnt++;
  179.  
  180.         met->tc_Node.ln_Pri=0;
  181.  
  182.         Signal(task,SIGBREAKF_CTRL_D);
  183.         while(quit_getidle) cnt=0;
  184.  
  185.         /* remove getidle()-task */
  186.  
  187.         Forbid();
  188.         DeleteTask(task);
  189.         Permit();
  190.     }
  191.  
  192.     idle   =100000;    /* to avoid divisions by zero from the       */
  193.     maximum=100000;    /* application, if creation of task failed.  */
  194.  
  195.     quit_setidle=FALSE;
  196.     Wait(0L);
  197. }
  198.  
  199. struct Task *init_idle()
  200. {
  201.     char *taskname_2 = "CPU_SET";
  202.     if( task2=(struct Task *)CreateTask(taskname_2,126,setidle,4096))
  203.     {
  204.         Delay(50L); /* To avoid the use of idle and maximum before they're initialized */
  205.     }
  206.     return(task2);
  207. }
  208.  
  209. free_idle()
  210. {
  211.     quit_setidle=TRUE;
  212.     Signal(task2,SIGBREAKF_CTRL_D);
  213.     while(quit_setidle) Delay(10L);
  214.  
  215.     /* remove setidle()-task */
  216.  
  217.     Forbid();
  218.     DeleteTask(task2);
  219.     Permit();
  220. }
  221.